import 'package:flutter/material.dart';
import '../pages/welcome.dart';
import '../pages/musicList.dart';
import '../pages/testUI.dart';


class BottomNav extends StatefulWidget {
    var data;

    BottomNav(data) {
        this.data = data;
    }
    
    @override
    State<StatefulWidget> createState() {
        return new BottomNavState(data);
    }
}

class BottomNavState extends State<BottomNav> {
    int _currentIndex = 0;

    BottomNavState(data) {
        _currentIndex = data;
    }
    
    @override
    Widget build(BuildContext context) {
        print('getBottomNav-currIndex=$_currentIndex');
        void _onItemTapped(int index) {
            print('BottomNav-OnTap-$index');
            
            if(_currentIndex == index) {
                return;
            }
            
            switch(index) {
                case 0:
                //Navigator.of(context).push(new MaterialPageRoute(
                //    builder: (ctx) => new MusicList()
                //));
                    Navigator.of(context).pushAndRemoveUntil(
                        new MaterialPageRoute(builder: (context) => new MusicList()
                        ), (route) => route == null);
                    break;
                case 1:
                    Navigator.of(context).pushAndRemoveUntil(
                        new MaterialPageRoute(builder: (context) => new TestUI()
                        ), (route) => route == null);
                    break;
                case 2:
                    Navigator.of(context).pushAndRemoveUntil(
                        new MaterialPageRoute(builder: (context) => new Welcome()
                        ), (route) => route == null);
                    break;
                default:
                    break;
            }
            
            setState(() {
                _currentIndex = index;
            });
        }
        return BottomNavigationBar(
            items: <BottomNavigationBarItem>[
                BottomNavigationBarItem(icon: Icon(Icons.queue_music), title: Text('音乐')),
                BottomNavigationBarItem(icon: Icon(Icons.android), title: Text('UI测试')),
                BottomNavigationBarItem(icon: Icon(Icons.account_box), title: Text('我的')),
            ],
            currentIndex: _currentIndex,
            fixedColor: Colors.blue,
            onTap: _onItemTapped,
        );
    }
}


class BottomNav2 {
    
    static getBottomNav(BuildContext context, int currIndex) {

        print('getBottomNav-currIndex=$currIndex');
        void _onItemTapped(int index) {
            print('BottomNav-OnTap-$index');
            switch(index) {
                case 0:
                    //Navigator.of(context).push(new MaterialPageRoute(
                    //    builder: (ctx) => new MusicList()
                    //));
                    Navigator.of(context).pushAndRemoveUntil(
                        new MaterialPageRoute(builder: (context) => new MusicList()
                        ), (route) => route == null);
                    break;
                case 1:
                    Navigator.of(context).pushAndRemoveUntil(
                        new MaterialPageRoute(builder: (context) => new TestUI()
                        ), (route) => route == null);
                    break;
                case 2:
                    Navigator.of(context).pushAndRemoveUntil(
                        new MaterialPageRoute(builder: (context) => new Welcome()
                        ), (route) => route == null);
                    break;
                default:
                    break;
            }
        }
        
        return BottomNavigationBar(
            items: <BottomNavigationBarItem>[
                BottomNavigationBarItem(icon: Icon(Icons.queue_music), title: Text('音乐')),
                BottomNavigationBarItem(icon: Icon(Icons.android), title: Text('UI测试')),
                BottomNavigationBarItem(icon: Icon(Icons.account_box), title: Text('我的')),
            ],
            currentIndex: currIndex,
            fixedColor: Colors.blue,
            onTap: _onItemTapped,
        );
    }
}
